<script setup>
import { useMyToastStore } from "@/store/myToast";
import { getCurrentInstance, ref } from "vue";
import jsrsasign from "jsrsasign";
const instance = getCurrentInstance();
const util = instance?.proxy?.$util;

const myToast = useMyToastStore();
const baseUrl = "http://192.168.2.139:37913";
const getPublicKey = (password) => {
  return new Promise(async (resolve, reject) => {
    console.log(2);
    const res = await uni.request({
      method: "GET",
      url: `${baseUrl}/daily8am/publicKey`,
    });
    console.log(res);
    // 密码加密
    let modulus = res.data.modulus;
    let exponent = res.data.exponent;
    let rsaKey = new jsrsasign.RSAKey();
    rsaKey.setPublic(jsrsasign.b64tohex(modulus), jsrsasign.b64tohex(exponent));
    let encrypt = jsrsasign.hex2b64(rsaKey.encrypt(password));
    // 请求Cookie
    let setCookie = res.cookies[0];
    let cookie = setCookie.substring(
      setCookie.indexOf("JSESSIONID"),
      setCookie.indexOf(";")
    );
    // 获取 csrftoken
    const csrftoken = await getCsrfToken(cookie);
    resolve({ encrypt, cookie, csrftoken });
  });
};
// 获取csrftoken
const getCsrfToken = (cookie) => {
  return new Promise(async (resolve, reject) => {
    const res = await uni.request({
      method: "POST",
      url: `${baseUrl}/daily8am/csrfToken`,
      data: {
        cookie,
      },
    });
    const index = res.data.indexOf("csrftoken");
    let html = res.data.substr(index + 1, 150);
    let token = html.substring(0, html.indexOf("/>"));
    let csrftoken = token.substr(token.indexOf("value"));
    token = csrftoken.substr(csrftoken.indexOf("=")).slice(2, -1);
    resolve(token);
  });
};
const testRequst = async () => {
  console.log(1);
  const loginData = await getPublicKey("AZe@8639");
  console.log(loginData);
  // 登录
  const res = await uni.request({
    method: "POST",
    url: `${baseUrl}/daily8am/login`, //仅为示例，并非真实接口地址。
    data: {
      password: loginData.encrypt,
      account: "22055050428",
      csrftoken: loginData.csrftoken,
      cookie: loginData.cookie,
    },
  });
  console.log(res);
  // schoolCalendar
  const courseList = await uni.request({
    method: "POST",
    url: `${baseUrl}/daily8am/adjustCourseList`, //仅为示例，并非真实接口地址。
    data: {
      cookie: res.data,
      id: "EDB498F941A4319DE055000000000001",
      account: "22055050428",
    },
  });
  let html = courseList.data;
  let modelList = html.slice(html.indexOf("modelList"), -1);
  let adjustCourseList = modelList.slice(
    modelList.indexOf("('") + 2,
    modelList.indexOf("');")
  );
  console.log(JSON.parse(adjustCourseList));
};
let toast = ref(null);
const testRequst2 = async () => {
  const res = await uni.request({
    method: "GET",
    url: `https://api.rebang.today/v1/items?tab=top&sub_tab=today&page=1`, //仅为示例，并非真实接口地址。
  });
  let list = res.data.data.list;

  console.log(JSON.parse(list));
};
</script>

<template>
  <view class="test">
    <myToast></myToast>
    <!-- <u-toast ref="toast"></u-toast> -->
    <button @click="testRequst">test</button>
    <button @click="testRequst2">test2</button>
  </view>
</template>

<style scoped lang="scss">
.test {
  padding-top: 200px;
}
</style>
